package net.pimpas.report.processor.condition.impl;

import net.pimpas.report.processor.condition.AbstractCondition;
import net.pimpas.report.processor.condition.Condition;
import net.pimpas.report.processor.condition.ConditionType;
import net.pimpas.report.processor.exception.ConditionException;
import net.pimpas.report.processor.exception.FieldValidationException;
import net.pimpas.report.processor.resolver.FieldPropertyResolver;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

public class OrCondition extends AbstractCondition {

	/**
	 * 
	 */
	private static final long serialVersionUID = 607572071274130682L;
	private Condition condition1;
	private Condition condition2;
	
	public OrCondition(Condition condition1, Condition condition2) {
		super(null, ConditionType.OR);
		this.setCondition1(condition1);
		this.setCondition2(condition2);
	}

	@Override
	public Criterion process(String fieldAlias, FieldPropertyResolver resolver) throws ConditionException {
		return Restrictions.or(this.getCondition1().process(resolver.resolve(this.getCondition1().getField()), resolver),
				this.getCondition2().process(resolver.resolve(this.getCondition2().getField()), resolver));
	}
	
	@Override
	public void validate(FieldPropertyResolver resolver) throws FieldValidationException {
		super.validate(resolver);
		
		this.getCondition1().validate(resolver);
		this.getCondition2().validate(resolver);
	}

	public Condition getCondition1() {
		return condition1;
	}

	public void setCondition1(Condition condition1) {
		this.condition1 = condition1;
	}

	public Condition getCondition2() {
		return condition2;
	}

	public void setCondition2(Condition condition2) {
		this.condition2 = condition2;
	}
}